package com.woniuxy.joylifecenter.service.impl;

import com.woniuxy.common.entity.*;
import com.woniuxy.common.mapper.ForumAdminApplyMapper;
import com.woniuxy.common.mapper.ForumAdminMapper;
import com.woniuxy.common.mapper.ForumForumMapper;
import com.woniuxy.joylifecenter.exception.CenterException;
import com.woniuxy.joylifecenter.service.ForumInitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ForumInitServiceImpl implements ForumInitService {

    @Autowired
    ForumForumMapper forumForumMapper;
    @Autowired
    ForumAdminApplyMapper forumAdminApplyMapper;
    @Autowired
    ForumAdminMapper forumAdminMapper;

    /**
     * @param forumForum
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/3
     * @Param [forumForum]
     * @Description 创建社区
     */
    @Override
    public int forumInit(ForumForum forumForum) {
        if (forumForumMapper.isExisted(forumForum.getName(), forumForum.getPhone(), forumForum.getAddress()).size() == 0) {
            return forumForumMapper.insertSelective(forumForum);
        } else {
            throw new CenterException("社区记录已存在，请勿重复添加");
        }

    }

    /**
     * @param forumForum
     * @return com.woniuxy.common.entity.ForumForum
     * @Author ArmWatt
     * @Date 2020/5/3
     * @Param [forumForum]
     * @Description 已创建社区多条件模糊查询
     */
    @Override
    public List<ForumForum> selectForumByCondition(ForumForum forumForum) {
        ForumForumExample example = new ForumForumExample();
        ForumForumExample.Criteria criteria = example.createCriteria();
        if (forumForum.getId() != null && !(forumForum.getId().equals(""))) {
            criteria.andIdLike("%" + forumForum.getId() + "%");
        }
        if (forumForum.getName() != null && !(forumForum.getName().equals(""))) {
            criteria.andNameLike("%" + forumForum.getName() + "%");
        }
        if (forumForum.getAddress() != null && !(forumForum.getAddress().equals(""))) {
            criteria.andAddressLike("%" + forumForum.getAddress() + "%");
        }
        if (forumForum.getDate() != null && !(forumForum.getDate().equals(""))) {
            criteria.andDateGreaterThanOrEqualTo(forumForum.getDate());
        }
        if (forumForum.getPhone() != null && !(forumForum.getPhone().equals(""))) {
            criteria.andPhoneLike("%" + forumForum.getPhone() + "%");
        }
        example.setOrderByClause("'date' DESC");
        return forumForumMapper.selectByExample(example);

    }

    /**
     * @param ids
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/4
     * @Param [id]
     * @Description 手动批量删除社区
     */
    @Override
    public int batchDeleteForumManually(List<String> ids) {
        ForumForumExample example = new ForumForumExample();
        ForumForumExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        return forumForumMapper.deleteByExample(example);
    }

    /**
     * @param id
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/4
     * @Param [id]
     * @Description 手动删除单个社区
     */
    @Override
    public int deleteForumManually(String id) {
        return forumForumMapper.deleteByPrimaryKey(id);
    }

    /**
     * @param forumForum
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/5
     * @Param [forumForum]
     * @Description 编辑社区记录
     */
    @Override
    public int updateForum(ForumForum forumForum) {

        if (forumForumMapper.isExistedForEdit(
                forumForum.getId(),
                forumForum.getName(),
                forumForum.getPhone(),
                forumForum.getAddress()).size() == 0) {
            ForumForumExample example = new ForumForumExample();
            ForumForumExample.Criteria criteria = example.createCriteria();
            criteria.andNameEqualTo(forumForum.getName());
            criteria.andPhoneEqualTo(forumForum.getPhone());
            criteria.andAddressEqualTo(forumForum.getAddress());
            return forumForumMapper.updateByPrimaryKeySelective(forumForum);
        } else {
            throw new CenterException("与已有社区信息重复，无法完成修改");
        }


    }

    /**
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Author ArmWatt
     * @Date 2020/5/12
     * @Param []
     * @Description 查询所有待审批的社区管理员申请
     **/
    @Override
    public List<Map<String, Object>> queryAllForumAdminApply() {
        return forumAdminApplyMapper.queryAllWaitApply();
    }

    /**
     * @param id
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/13
     * @Param [id]
     * @Description 审核不过
     */
    @Override
    public int rejectForumAdminApply(String id) {
        return forumAdminApplyMapper.rejectApply(id);
    }

    /**
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/13
     * @Param [id, userid, forumid]
     * @Description 审核通过
     **/
    @Override
    public int passForumAdminApply(String id, String userid, String forumid) {
        int count = forumAdminApplyMapper.passApply(id);
        addForumAdmin(userid, forumid);
        return count;
    }

    /**
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/13
     * @Param [userid, forumid]
     * @Description 添加社区吧主关联记录
     **/
    @Override
    public int addForumAdmin(String userid, String forumid) {
        ForumAdmin forumAdmin = new ForumAdmin(UUID.randomUUID().toString().replaceAll("-", ""), userid, forumid);
        return forumAdminMapper.insert(forumAdmin);

    }


}
